﻿using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using System;
namespace ConfExpert.Forms.DockingControls
{
    public class DockingControlBase
    {
        public void AddMenuItem(NavBarGroup group, string caption, Type type, params object[] prm)
        {
            NavBarItem menu = new NavBarItem(caption);
            menu.LinkClicked += delegate { AppSession.MainForm.ShowMdiChildForm(type, prm); };
            menu.SmallImage = Properties.Resources.btn1y;
            group.ItemLinks.Add(menu);
            group.Expanded = true;
        }

        public void AddMenuItem(NavBarGroup group, string caption, Action Method)
        {
            NavBarItem menu = new NavBarItem(caption);
            menu.LinkClicked += delegate { Method(); };
            menu.SmallImage = Properties.Resources.btn1y;
            group.ItemLinks.Add(menu);
            group.Expanded = true;
        }

        public NavBarGroup AddGroup(string caption)
        {
            NavBarGroup group = NavigationBar.Groups.Add(new DevExpress.XtraNavBar.NavBarGroup(caption));
            return group;
        }

        public NavBarControl NavigationBar;
        public GridLookUpEdit Lookup;
        private SimpleButton clearFilterButton;

        public SimpleButton ClearFilterButton
        {
            get { return clearFilterButton; }
            set
            {
                clearFilterButton = value;
                clearFilterButton.Click += new EventHandler(clearFilterButton_Click);
            }
        }

        void clearFilterButton_Click(object sender, EventArgs e)
        {
            filterTextBox.Text = string.Empty;
        }
        private TextEdit filterTextBox;

        public TextEdit FilterTextBox
        {
            get { return filterTextBox; }
            set
            {
                filterTextBox = value;
                filterTextBox.TextChanged += new EventHandler(filterTextBox_TextChanged);
            }
        }

        void filterTextBox_TextChanged(object sender, EventArgs e)
        {
            if (FilterTextBox.Text == string.Empty)
            {
                foreach (NavBarItem nbi in NavigationBar.Items)
                    nbi.Visible = true;

                foreach (NavBarGroup grp in NavigationBar.Groups)
                {
                    grp.Caption = grp.Caption.Split('(')[0];
                    grp.Visible = true;
                }
            }
            else
            {
                foreach (NavBarItem nbi in NavigationBar.Items)
                    nbi.Visible = nbi.Caption.ToLower().Contains(FilterTextBox.Text.ToLower());

                foreach (NavBarGroup grp in NavigationBar.Groups)
                {
                    grp.Caption = grp.Caption.Split('(')[0].Trim() + " (" + grp.VisibleItemLinks.Count + ")";
                    grp.Visible = grp.VisibleItemLinks.Count > 0;
                }
            }
        }



    }
}
